package com.sskj.login.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.support.transition.ChangeClipBounds;
import android.support.transition.Scene;
import android.support.transition.TransitionManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.TextInput;
import com.github.yoojia.inputs.WidgetAccess;
import com.hey.lib.HeySpinner;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.enums.AreaCodeEnum;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.login.util.AreaCodeUtil;
import com.sskj.lib.util.CaptchaUtil;
import com.sskj.lib.util.EditUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.lib.util.SendCodeUtil;
import com.sskj.login.R;
import com.sskj.login.R2;
import com.sskj.login.bean.LoginBean;
import com.sskj.login.component.DaggerUserDataComponent;
import com.sskj.login.presenter.LoginActivityPresenter;
import com.sskj.login.util.TipLoginUtil;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 登录
 */
@Route(path = RConfig.LOGIN_LOGIN)
public class LoginActivity extends BaseActivity<LoginActivityPresenter> {
    @BindView(R2.id.tvRegister)
    TextView tvRegister;
    @BindView(R2.id.etAccount)
    EditText etAccount;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.btLogin)
    Button btLogin;
    @BindView(R2.id.tvPwdForget)
    TextView tvPwdForget;
    @BindView(R2.id.tvPhone)
    TextView tvPhone;
    @BindView(R2.id.tvEmail)
    TextView tvEmail;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.llTab)
    LinearLayout llTab;
    @BindView(R2.id.includeAreaCode)
    LinearLayout includeAreaCode;

    private AndroidNextInputs inputs;
    @Inject
    UserViewModel userViewModel;
    private BottomSheetDialog googleCheckDialog;
    private AreaCodeEnum areaCodeEnumChoose;
    @BindView(R2.id.heySpinner)
    HeySpinner heySpinner;
    private WidgetAccess widgetAccess;

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity_login;
    }

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    public LoginActivityPresenter getPresenter() {
        return new LoginActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        LiveDataBus.get().with(RxBusCode.CHANGE_MAIN_TAB, Integer.class).postValue(0);
        inputs = new ToastNextInputs();

        etPwd.setHint(getString(R.string.loginlogin_include_pwd40));
        widgetAccess = new WidgetAccess(this);
        TextInput<EditText> editTextAccount = (TextInput<EditText>) widgetAccess.findEditText(R.id.etAccount).setPriority(-1);

        new AreaCodeUtil().init(this, App.INSTANCE.getString(R.string.login_loginActivity2), App.INSTANCE.getString(R.string.login_loginActivity3), llTab, includeAreaCode, etAccount, editTextAccount, inputs);
        inputs.clear()
                .add(editTextAccount, SchemeUtil.notEmpty(etAccount), SchemeUtil.phone())
                .add(widgetAccess.findEditText(R.id.etPwd), SchemeUtil.notEmpty(etPwd), SchemeUtil.pwd());
        ClickUtil.click(tvRegister, () -> {
            ARouter.getInstance().build(RConfig.LOGIN_REGISTER).navigation();
        });


        ClickUtil.click(btLogin, () -> {
            if (inputs.test()) {
                mPresenter.login(areaCodeEnumChoose.getAreaCode(), etAccount.getText().toString(), etPwd.getText().toString(), "APP");
            }
        });

        EditUtil.togglePs(etPwd, ivPwdToggle);

        ClickUtil.click(tvPwdForget, () -> {
            ARouter.getInstance().build(RConfig.LOGIN_PWD_FORGET).navigation();
        });

        SendCodeUtil.areaCode(this, heySpinner, areaCodeEnum -> {
            areaCodeEnumChoose = areaCodeEnum;
        });
    }

    public void loginSuccess(LoginBean data) {
        ToastUtil.showShort(getString(R.string.login_state_success));
        saveDataAndLogin(data);
        mPresenter.sendLoginSms(etAccount.getText().toString(), data.getId(), data.getToken());
    }

    public void saveDataAndLogin(LoginBean data) {
        HttpHeaders httpHeaders = OkGo.getInstance().getCommonHeaders();
        HttpParams httpParams = OkGo.getInstance().getCommonParams();
        httpHeaders.put(SPConfig.TOKEN, data.getToken());
        httpHeaders.put(SPConfig.HEADER_ID, data.getId());
        httpParams.put(SPConfig.ID, data.getId());
        httpParams.put(SPConfig.STOCK_USER_ID, data.getId());
        SPUtil.put(SPConfig.ID, data.getId());
        SPUtil.put(SPConfig.USER_ACCOUNT, etAccount.getText().toString());
        SPUtil.put(SPConfig.TOKEN, data.getToken());
        SPUtil.put(SPConfig.MOBILE, etAccount.getText().toString());
        SPUtil.put(SPConfig.GOOGLETYPE, data.getIsGoogleAuthentication());
        SPUtil.put(SPConfig.LOGIN, true);
        SPUtil.put(SPConfig.AREA_CODE, areaCodeEnumChoose.getAreaCode());
        userViewModel.update();
        finish();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);

    }

    public void googleCheck(LoginBean loginBean) {
//        CaptchaUtil.check(this, (result, validate, msg) -> {
            googleCheckDialog = BottomSheetUtil.showCheck(LoginActivity.this, null, false, true, false, null, (pwd, googleCode, smsCode) -> {
                googleCheckDialog.dismiss();
                mPresenter.checkGoogle(googleCode, loginBean);
            });
            googleCheckDialog.show();

//        });
    }

    public void checkSuccess(LoginBean data) {
        saveDataAndLogin(data);
        mPresenter.sendLoginSms(etAccount.getText().toString(), data.getId(), data.getToken());
    }
}
